import 'package:shared_preferences/shared_preferences.dart';

/// key-value 缓存
class KVCacheStorage {
  static final KVCacheStorage _instance = KVCacheStorage._();

  KVCacheStorage._();

  factory KVCacheStorage() {
    return _instance;
  }

  final _kv = SharedPreferencesAsync();

  Future<void> setBool(String key, bool value) async {
    _kv.setBool(key, value);
  }

  Future<void> setInt(String key, int value) async {
    _kv.setInt(key, value);
  }

  Future<void> setDouble(String key, double value) async {
    _kv.setDouble(key, value);
  }

  Future<void> setString(String key, String value) async {
    _kv.setString(key, value);
  }

  Future<void> setStringList(String key, List<String> value) async {
    _kv.setStringList(key, value);
  }


  Future<int> getInt(String key,{int defaultValue = 0}) async {
    return await _kv.getInt(key) ?? defaultValue;
  }

  Future<bool> getBool(String key,{bool defaultValue = false}) async {
    return await _kv.getBool(key) ?? defaultValue;
  }

  Future<double> getDouble(String key,{double defaultValue = 0}) async {
    return await _kv.getDouble(key) ?? defaultValue;
  }


  Future<String> getString(String key,{String defaultValue = ""}) async {
    return await _kv.getString(key) ?? defaultValue;
  }

  Future<List<String>> getStringList(String key,{List<String>? defaultValue}) async {
    return await _kv.getStringList(key) ?? defaultValue ?? [];
  }

  // 移除 key
  Future<void> remove(String key) async {
    await _kv.remove(key);
  }

  // 清除所有缓存
  Future<void> clear() async {
    await _kv.clear();
  }

  // 判断是否包含 key
  Future<bool> containsKey(String key) async {
    return await _kv.containsKey(key);
  }


}